package io.geekidea.boot.vipmanagement.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.geekidea.boot.common.PageDto;
import io.geekidea.boot.framework.response.ApiResult;
import io.geekidea.boot.vipmanagement.dto.VipmanagementDto;
import io.geekidea.boot.vipmanagement.entity.Vipmanagement;
import io.geekidea.boot.vipmanagement.query.VipmanagementQuery;
import io.geekidea.boot.vipmanagement.service.VipmanagementService;
import io.geekidea.boot.viptype.entity.Viptype;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.math3.ml.neuralnet.UpdateAction;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/vipmanagement")
@Tag(name="套餐管理")
public class VipmanagementController {
    @Resource
    private VipmanagementService vipmanagementService;

    @PostMapping("/search")
    @Operation(summary = "搜索套餐",description = "按照多条件组合分页查询套餐列表")
    public ApiResult search(@RequestBody VipmanagementQuery vipmanagementQuery){
        Page<Vipmanagement> page=new Page<>(vipmanagementQuery.getPage(),vipmanagementQuery.getSize());
        Page<Vipmanagement> pageDto = vipmanagementService.vipmanagementPage(page, vipmanagementQuery);
        PageDto<Vipmanagement> pageInfo=new PageDto<>(pageDto.getRecords(),pageDto.getTotal(),pageDto.getSize(),pageDto.getCurrent());
        return ApiResult.success(pageInfo);
    }

    @PostMapping("/getviptype")
    @Operation(summary = "获取套餐类型")
    public ApiResult getviptype(){
        List<Viptype> viptypeList = vipmanagementService.getAll();
        return ApiResult.success(viptypeList);
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "根据ID查询单条数据")
    public ApiResult getVipmanagementById(@PathVariable(value = "id")Integer id){
        final Vipmanagement Vipmanagement=vipmanagementService.getById(id);
        return ApiResult.success(Vipmanagement);
    }

    @DeleteMapping("/romove/{id}")
    @Operation(summary = "逻辑删除套餐")
    public ApiResult removeArea(@PathVariable(value = "id")Integer id){
        vipmanagementService.removeById(id);
        return ApiResult.success("删除套餐数据成功！");
    }

    @PostMapping("/add")
    public ApiResult addArea(@RequestBody VipmanagementDto vipmanagementDto){
        Vipmanagement vipmanagementEntity=new Vipmanagement();
        BeanUtil.copyProperties(vipmanagementDto,vipmanagementEntity);
        vipmanagementService.save(vipmanagementEntity);
        return ApiResult.success("添加套餐数据成功！");
    }

    //逻辑删除
    @GetMapping("/del/{id}")
    @Operation(summary = "物理删除",description = "根据id逻辑删除对应的列")
    public ApiResult del(@PathVariable Integer id){
        boolean update = vipmanagementService.delById(id);
        if (!update) {
            return ApiResult.success("删除失败");
        }
        return ApiResult.success("删除成功");

    }


    @PostMapping("/update")
    public ApiResult updateArea(@RequestBody VipmanagementDto vipmanagementDto){

        boolean b = vipmanagementService.updateId(vipmanagementDto);

        if (!b) {
            return ApiResult.success("更改套餐数据失败！");
        }

        return ApiResult.success("更改套餐数据成功！");
    }
}
